package thread;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import static javafx.scene.input.KeyCode.J;

/**
 * 文件复制
 */
public class FileCopy extends JFrame implements ActionListener {
    private JTextField tf1, tf2;
    private JProgressBar pb;
    private JButton bt1, bt2, cp;
     private File srcFile=null;
    private  String desPath=null;

    public FileCopy() {//构造方法
        setTitle("文件复制");
        setSize(600, 400);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(null);
        JLabel label1 = new JLabel("选择文件");//标签
        JLabel label2 = new JLabel("复制到");
        label1.setBounds(50, 50, 60, 25);
        label2.setBounds(50, 100, 60, 25);
        add(label1);
        add(label2);
        tf1 = new JTextField();
        tf2 = new JTextField();//编辑文本
        tf1.setBounds(110, 50, 300, 25);
        tf2.setBounds(110, 100, 300, 25);
        tf1.setEnabled(false);
        tf2.setEnabled(false);
        add(tf1);
        add(tf2);
         bt1 = new JButton("...");//创建按钮
         bt2 = new JButton("...");
        bt1.setBounds(420, 50, 60, 25);
        bt2.setBounds(420, 100, 60, 25);
        bt1.addActionListener(this);
        bt2.addActionListener(this);
        add(bt1);
        add(bt2);
        cp = new JButton("复制");
        cp.setBounds(220, 250, 60, 25);
        cp.addActionListener(this);
        add(cp);
        pb = new JProgressBar();//创建进度条
        pb.setStringPainted(true);
        pb.setBounds(60, 175, 400, 25);
        //pb.setValue(50);
        add(pb);

    }



    @Override
    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();
        if (o == bt1) {//选择文件
            JFileChooser fc = new JFileChooser();
            //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int f = fc.showOpenDialog(this);//f为0代表打开了文件
            if (f == JFileChooser.APPROVE_OPTION) {
                srcFile = fc.getSelectedFile();
                System.out.println();
                tf1.setText(srcFile.getPath());
            }


        } else if (o == bt2) {//选择目的文件夹
            JFileChooser fc = new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int f = fc.showOpenDialog(this);//f为0代表打开了文件
            if (f == JFileChooser.APPROVE_OPTION) {
                desPath = fc.getSelectedFile().getPath();
                System.out.println();
                tf2.setText(desPath);
            }

        } else if (o == cp) {//开始复制
            if (srcFile == null || desPath == null) {
                JOptionPane.showMessageDialog(this, "请选择文件");
                return;
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    File desFile = new File(desPath + "\\" + srcFile.getName());
                    try {
                        FileInputStream fis = new FileInputStream(srcFile);
                        FileOutputStream fos = new FileOutputStream(desFile);
                        byte[] tmp = new byte[2048];
                        int c = fis.read(tmp);
                        long fsize = srcFile.length();
                        int percent = 0;
                        while (c != -1) {
                            fos.write(tmp, 0, c);
                            percent = ((int) (1.0 - 1.0 * fis.available() / fsize) * 100);
                            pb.setValue(percent);
                            c = fis.read(tmp);
                        }
                        fos.close();
                        fis.close();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }

            }).start();
        }
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FileCopy();
            }
        });
    }

}
